package zym.diandian.module.wireless;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.stericson.RootTools.RootTools;
import com.stericson.RootTools.RootToolsException;

import java.io.IOException;

import zym.diandian.R;
import zym.diandian.tools.NetUtil;
import zym.diandian.tools.Props;

//adb 无线调试
public class WirelessFrame extends Fragment implements View.OnClickListener {
    private View content;
    private Button switch_button;
    private View line;
    private TextView console;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.content = inflater.inflate(R.layout.wireless_fragment, null);

        getActivity().setTitle("无线调试(ROOT/WIFI)");

        return this.content;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        this.switch_button = (Button) content.findViewById(R.id.wireless_button);
        this.switch_button.setOnClickListener(this);

        this.line = content.findViewById(R.id.wireless_line);

        this.console = (TextView) content.findViewById(R.id.wireless_console);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (switch_button != null && console != null) {
            if (isOpen() != null) {
                this.switch_button.setText("关闭无线调试");
                console.setText("");
                console.append("\r\nPC 端命令行输入  adb connect " + NetUtil.getWifiIp(getActivity()) + ":5555 进行连接");
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.wireless_button:

                if (isOpen() != null) {
                    //关闭无线调试
                    String command[] = new String[]{"setprop service.adb.tcp.port " + 0, "stop adbd", "start adbd"};
                    try {
                        RootTools.sendShell(command, 2000);
                        if (isOpen() == null) {
                            console.append("\r\n关闭无线调试成功");
                            this.switch_button.setText("一键开启无线调试");
                        } else {
                            console.append("\r\n关闭无线调试失败");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        console.append("\r\n关闭无线调试失败");
                    }
                    return;
                }

                //1.判断WIFI是否已经打开并连接
                console.setText("");
                if (NetUtil.isWifiConnect(getActivity())) {
                    console.append("1.WIFI 已连接");
                    //2.判断是否已经开启过无线调试,通过端口检测
                    if (isOpen() == null) {
                        console.append("\r\n2.开始开启无线调试....");
                        String command[] = new String[]{"setprop service.adb.tcp.port " + 5555, "stop adbd", "start adbd"};
                        try {
                            RootTools.sendShell(command, 2000);
                            //3.检测是否开启成功
                            if (isOpen() != null) {
                                console.append("\r\n3.开启成功：\r\n");
                                console.append("\r\nPC 端命令行输入  adb connect " + NetUtil.getWifiIp(getActivity()) + ":5555 进行连接");
                                switch_button.setText("关闭无线调试");
                            }
                        } catch (Exception e) {
                            console.append("\r\n开启无线调试失败：\r\n" + e.getMessage());
                            e.printStackTrace();
                        }
                    } else {
                        console.append("\r\n2.已开启无线调试模式");
                    }
                } else {
                    console.append("1.WIFI 未连接,请打开WIFI并和电脑连接至同一WIFI(或 电脑创建WIFI手机连接)");
                }


                break;
            default:
                break;
        }
    }


    //判断无线调试是否已经打开
    private String isOpen() {
        String port = NetUtil.getProp(Props.adb_port);
        if (TextUtils.isEmpty(port)) {
            return null;
        } else {
            if ("0".equals(port)) {
                return null;
            }
            return port;
        }
    }

}
